<template>
  <admin-layout class="ArticleShow">
    <h1>ArticleUpdate</h1>
    <el-form :model="articleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="标题" prop="title">
        <el-input v-model="articleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input type="textarea" v-model="articleForm.desc"></el-input>
      </el-form-item>
      <el-form-item label="文章详情" prop="editor">
        <div ref="editorDom" style="text-align:left
        ">富文本剪辑器</div>
      </el-form-item>
      <el-form-item label="发布者" prop="author">
        <el-select v-model="articleForm.author" placeholder="请选择发布者">
          <el-option label="人民日报" value="人民日报"></el-option>
          <el-option label="美国日报" value="美国日报"></el-option>
          <el-option label="一盏日报" value="一盏日报"></el-option>
          <el-option label="超厉害日报" value="超厉害日报"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间" required>
        <el-form-item prop="created_at">
          <el-date-picker type="date" placeholder="选择发布日期" v-model="articleForm.created_at"></el-date-picker>
        </el-form-item>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">修改保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </admin-layout>
</template>

<script>
  import Editor from 'wangeditor'
  import {
    mapState
  } from 'vuex'
  export default {
    name: "ArticleCreate",
    computed: {
      ...mapState('article', ['item'])
    },
    watch: {
      item() {
        // this.articleForm = { ...this.item }
        this.articleForm = Object.assign({}, this.item)
      }
    },
    data() {
      return {
        articleForm: {
          title: '',
          desc: '',
          author: '',
          created_at: '',
          editor: ''
        },
        rules: {
          title: [{
              required: true,
              message: '请输入文章标题',
              trigger: 'blur'
            },
            {
              min: 5,
              max: 20,
              message: '长度在 5 到 20 个字符',
              trigger: 'change'
            }
          ],
          desc: [{
            required: true,
            message: '请填写文章描述',
            trigger: 'blur'
          }],
          author: [{
            required: true,
            message: '请选择发布者',
            trigger: 'change'
          }],
          created_at: [{
            type: 'date',
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }],
          editor: [{
            // required: true,
            // trigger: 'blur',
            // message: '请填写文章内容'
          }],
        }
      }
    },
    mounted() {
      this.createEditor()
    },
    async created() {
      // articleForm
      // 数据回填
      const {
        id
      } = this.$route.params
      await this.$store.dispatch('article/getItem', id)
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            // alert('submit!');
            // console.log(this.articleForm)
            try {
              await this.$store.dispatch('article/updateArticle', {
                id: this.$route.params.id,
                data: this.articleForm
              })
              this.$message.success('修改成功')
              this.$router.push({
                name: 'Article'
              })
            } catch (e) {
              this.$message.success('网络错误')
            }
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      createEditor() {
        this.editor = new Editor(this.$refs.editorDom)
        this.editor.create() //创建富文本实例
      }
    }
  };
</script>

<style scoped>

</style>